import 'dart:io';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/services.dart';

abstract class AppInstall {
  Future<void> install(String appPkgPath);

  factory AppInstall.instance() {
    if (Platform.isAndroid) {
      return AndroidAppInstall();
    }
    return DefaultAppInstall();
  }
}

class DefaultAppInstall implements AppInstall {
  @override
  Future<void> install(String appPkgPath) async {
    BotToast.showText(text: '暂未支持');
  }
}

class AndroidAppInstall implements AppInstall {
  static const MethodChannel _channel = MethodChannel('open_apk');

  @override
  Future<void> install(String appPkgPath) async {
    try {
      final bool success =
          await _channel.invokeMethod('openApk', {'path': appPkgPath});
      if (success) {
        BotToast.showText(text: '打开apk成功');
      } else {
        BotToast.showText(text: '打开apk失败');
      }
    } on PlatformException catch (e) {
      BotToast.showText(text: '打开apk失败，系统异常');
    }
  }
}
